package cn.tangwei.product.cotroller;

import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.product.domain.ProductDetail;
import cn.tangwei.product.query.ProductDetailQuery;
import cn.tangwei.product.service.IProductDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/productDetail")
public class ProductDetailController {

    @Autowired
    private IProductDetailService productDetailService;

    /**
     * 新增或修改服务详情 put
     * 传对象就用@RequestBody
     * @param productDetail
     * @return
     */
    @ApiOperation(value = "服务详情添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxRusult addOrupdate(@RequestBody ProductDetail productDetail){
        try {
            if(productDetail.getId() == null){
                productDetailService.add(productDetail);
            }else {
                productDetailService.update(productDetail);
            }
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 删除服务详情 delete
     * 传值就用@PathVariable("id")再josn里面拿数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxRusult del(@PathVariable("id") Long id){
        try {
            productDetailService.delete(id);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxRusult patchDel(@RequestBody List<Long> ids){
        try {
            productDetailService.patchDelete(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 查询一个服务详情 get
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ProductDetail queryById(@PathVariable("id") Long id){
        return productDetailService.queryById(id);
    }

    /**
     * 查询全部服务详情  get
     * @return
     */
    @GetMapping
    public List<ProductDetail> queryAll(){
        return productDetailService.queryAll();
    }

    /**
     * 分页加高级查询 post
     * @param query
     * @return
     */
    @PostMapping
    public PageList<ProductDetail> queryPage(@RequestBody ProductDetailQuery query){
        return productDetailService.queryPage(query);
    }
}
